import {
    reqgetgoodlist
} from "../../http/api";
let oldid=null;

// 初始值
const initState = {
    arr: []
}

//actionTYpes
const types = {
    change_arr: "list_change_arr"
}

//reducer 
const reducer = (state = initState, action) => {
    switch (action.type) {
        case types.change_arr:
            return {
                ...state,
                arr: action.arr
            }
            default:
                return state;
    }
}

//actionCreators
export const actions = {
    changeArr: (arr) => ({
        type: types.change_arr,
        arr
    }),
    reqArr: (id) => (dispatch, getState) => {
        if(oldid===id){
            return;
        }
        oldid=id;
        reqgetgoodlist({
            cateid: id,
            type: 1
        }).then(res => {
            let arr=res.data.list.goodData?res.data.list.goodData:[]
            dispatch(actions.changeArr(arr))
        })
    }
}

//selector 导出数据
export const getArr = state => state.list.arr;

//导出reducer
export default reducer